<?php
session_start();
include_once("function_right.php");
if(!JudgeUserRight()) return FALSE;

include_once "function_sec_nat.php";

$in_array['Name'] = $_POST['name'];
$in_array['NatType'] = $_POST['add_switch'];
$in_array['Enabled'] = $_POST['enable'];
$in_array['SrcAddr'] = $_POST['srcaddr'];
$in_array['SrcPort'] = $_POST['srcport'];
$in_array['DstAddr'] = $_POST['dstaddr'];
$in_array['DstPort'] = $_POST['dstport'];
$in_array['OutDev'] = $_POST['outdev'];
$in_array['InDev'] = $_POST['indev'];
$in_array['CvtSrcAddr'] = $_POST['cvtsrcaddr'];
$in_array['CvtDstAddr'] = $_POST['cvtdstaddr'];

$proto1 = $_POST['selPro'];
$proto2 = $_POST['protocode'];
$cvtsrcport1 = $_POST['cvtsrcport1'];
$cvtsrcport2 = $_POST['cvtsrcport2'];
$cvtdstport1 = $_POST['cvtdstport1'];
$cvtdstport2 = $_POST['cvtdstport2'];

if ($proto1 == "other")
	$in_array['Proto'] = $proto2;	// protocol number.
else
	$in_array['Proto'] = $proto1;	// All, Tcp, Udp, Icmp.

if ( $in_array['NatType'] != 2) {	// Snat, SDnat -> GetSourceInfo(SrcIp, SrcPort)
	if ( ($in_array['Proto'] != "tcp" && $in_array['Proto'] != "udp") || ($cvtsrcport1 == "" && $cvtsrcport2 == ""))
		$in_array['CvtSrcPort'] = "nochange";
	else {
		if ($cvtsrcport2 == "")
			$in_array['CvtSrcPort'] = $cvtsrcport1;
		else
			$in_array['CvtSrcPort'] = $cvtsrcport1. '-'. $cvtsrcport2;
	}
}
else {
	$in_array['CvtSrcAddr'] = "";
	$in_array['CvtSrcPort'] = "nochange";
	$in_array['OutDev'] = "All";
}

if ($in_array['NatType'] != 1) {	// Dnat, SDnat -> GetDstInfo(DstIp, DstPort)
	if ( ($in_array['Proto'] != "tcp" && $in_array['Proto'] != "udp") || ($cvtdstport1 == "" && $cvtdstport2 == ""))
		$in_array['CvtDstPort'] = "nochange";
	else {
		if ($cvtdstport2 == "")
			$in_array['CvtDstPort'] = $cvtdstport1;
		else
			$in_array['CvtDstPort'] = $cvtdstport1. '-'. $cvtdstport2;
	}
}
else {
	$in_array['CvtDstAddr'] = "";
	$in_array['CvtDstPort'] = "nochange";
	$in_array['InDev'] = "All";
}

$in_array['Type'] = "1";

$cnat = new CNat();
$cnat->edit_item_by_name($in_array['Name'], $in_array);
$cnat->Apply();

include_once "function_misc_log.php";
$in_array['Name'] = "'". $in_array['Name']. "'";
$in_array['SrcAddr'] = "'". $in_array['SrcAddr']. "'";
$in_array['DstAddr'] = "'". $in_array['DstAddr']. "'";
$in_array['CvtSrcAddr'] = "'". $in_array['CvtSrcAddr']. "'";
$in_array['CvtDstAddr'] = "'". $in_array['CvtDstAddr']. "'";
logger("Modify", $in_array, "Policy_Nat");

include "function_web_misc.php";
PrintSuccessPageAndJump('sec_policy_nat.php');

//echo("<meta http-equiv='refresh'content=0;URL='sec_policy_nat.php'>");
?>
